/**
 * 页面加载完成
 */
+(function(){ 
	var roleVue=new Vue({
		el:"#roleContainer",
		data:function(){
			return {
				loading:false,
				loadingTxt:$.admin.loadingTxt,
				treeHeight:pageContentHeight-50,
				tableHeight:pageContentHeight-100,
				treeData:{},
				treeProps:{
					children: 'children',
			        label: 'name'
				},
				treeStyle:{height:(pageContentHeight-50)+'px',overflow: "auto"},
				
				currentPage:1,
				defaultPageSize:defaultPageSize,
				pageSize:defaultPageSize,
				totalCount:0,
				qryForm:{
					name:""
				},
				tableData:[],
				tableSelectRow:null,
				
				editDiag:{
					title:"新增角色",
					type:1,
					form:{
						id:"",
						roleName:"",
						remark:""
					},
					show:false,
					formLabelWidth:"100px",
					rules:{
						roleName:[{ required: true, message: '角色名不能为空', trigger: 'blur' }]
					}
				}
			}
		},
		methods:{
			loadResoures:function(event)  //加载资源数据
			{
				var $this=this;
				var url=ctx+"/admin/resource/all";
				$.ajax({url:url,type:"POST",dataType:"json",data:{},success:function(data){
					var data=data.data;
					$this.treeData=$.util.listToTree(data,"id","parent");
				}});
			},
			
			qry:function(page) //查询
			{
				if(1!=page)
				{
					this.currentPage=1;
					this.totalCount=0;
				}
				this.loading=true;
				var url=ctx+"/admin/role/qry";
				var $this=this;
				var params={name:this.qryForm.name,pageIndex:this.currentPage-1,pageSize:this.pageSize};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					if(data && data.success==true)
					{
						var data=data.data;
						$this.totalCount=data.totalElements;
						$this.tableData=data.content;
					}
					$this.loading=false;
				}});
			},
			openAddPage:function(type,event){  //打开新增/修改页面
				this.editDiag.form;
				this.editDiag.type=type;
				this.editDiag.show=true;
				if(1==type)
				{
					this.editDiag.title="新增角色";
					this.editDiag.form.id="";
				}
				else if(2==type)
				{
					this.editDiag.title="修改角色";
					var url=ctx+"/admin/role/one";
					var $this=this;
					var $t=$(event.currentTarget);
					var params={id:$t.attr("roleId")};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						$this.editDiag.form=data.data;
					}});
				}
			},
			save:function(event) //保存
			{
				var $this=this;
				this.$refs["roleDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存成功";
						if($this.editDiag.type==1)
						{
							msg="保存成功";
						}
						else
						{
							msg="修改成功";
						}
						var url=ctx+"/admin/role/save";
						var params=$this.editDiag.form;
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.editDiag.show=false;
							$this.resetTable();
							$this.$message({
						          message: msg,
						          type: 'info'
						      });
						}});
					}
				});
			},
			del:function(event)  //删除角色 
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/role/del";
					var params={id:$t.attr("roleId")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.resetTable();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         }); 
						}
						else
						{
							$this.resetTable();
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			},
			
			onCloseEditDialog:function(event) //关闭弹出页面
			{
				this.clearForm();
			},
			clearForm:function() //清理新增表单数据
			{
				var $roleDialogForm =this.$refs["roleDialogForm"];
				if($roleDialogForm)
				{
					$roleDialogForm.resetFields();
				}
			},
			resetTable:function() //重置表格
			{
				this.qry();
				this.$refs["resourceTree"].setCheckedKeys([]); //清空树型选择
			},
			tableSelectChange:function(currentRow, oldCurrentRow)//表格选择改变事件
			{
				if(currentRow)
				{
					this.tableSelectRow=currentRow;
					this.selectRoleResourceTree();
				}
			},
			selectRoleResourceTree:function()
			{
				$this=this;
				var url=ctx+"/admin/role/rel/resource";
				var params={roleId:this.tableSelectRow.id}
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					var resourceIds=[];
					$this.$refs["resourceTree"].setCheckedKeys([]);
					for(var i=0;i<data.length;i++)
					{
						var resourceId=data[i].resourceId;
						resourceIds.push(resourceId);
						$this.$refs["resourceTree"].setChecked(resourceId,true);
					}
					//$this.$refs["resourceTree"].setCheckedKeys(resourceIds);
				}});
			},
			updateRoleResource:function(event) //更新角色资源
			{
				if(this.tableSelectRow!=null)
				{
					var $this=this;
					var $resourceTree=this.$refs["resourceTree"];
					var keys=[];
					//keys=$resourceTree.getCheckedKeys();
					//var checkNodes=$resourceTree.getCheckedNodes();
					keys=this.getCheckedResource($resourceTree.getCheckedNodes());
					var url=ctx+"/admin/role/save/resource";
					var params={roleId:this.tableSelectRow.id,resourceId:keys.toString()}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						var data=data.data;
						$this.$message({
				            type: 'info',
				            message: '修改角色权限成功'
				         }); 
					}});
				}
				else
				{
					this.$message({
			            type: 'warning',
			            message: '请选择角色'
			          });
				}
				
			},
			getCheckedResource:function(nodes) //获取选择的资源，包括父级
			{
				var keys=[];
				var objKeys={};
				for(var i=0;i<nodes.length;i++)
				{
					var node=nodes[i];
					objKeys[node.id]=node;
					if(node.parent && node.parent.id!='root')
					{
						objKeys[node.parent.id]=node.parent;
					}
				}
				for(var key in objKeys)
				{
					keys.push(key);
				}
				return keys;
			},
			handleSizeChange:function(val) {  //分页条改变显示条数
				this.qry(1);
			},
	        handleCurrentChange:function(val) { //分页条分页发生改变
	        	this.currentPage=val;
	        	this.qry(1);
	        }
		}
	});
	roleVue.qry();
	roleVue.loadResoures();
})();